import os

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class Login(webapp.RequestHandler):
    def get(self):
        goto = self.request.get("continue","/")
        domain = self.request.get("domain")
        
        if not domain or (domain=='google' or domain=='gmail'):
            oid = 'http://google.com/accounts/o8/id'
            url = users.create_login_url(goto,None,oid)
        else:
            oid = "http://google.com/accounts/o8/site-xrds?hd=%s" % (domain)
            url = users.create_login_url(goto,domain,oid)
        
        body=[("%s <a href=\"%s\">Sign in </a>.<br>" % (domain,url))]
        body.append("<h3>environ</h3>")
        for name in os.environ.keys():
            body.append("%s = %s<br>\n" % (name, os.environ[name]))
        
        body.append("<h3>Url Parameters</h3>")
        html = "".join(body) + self.footer()
        
        self.response.out.write("<html><body>%s</body></html>" % html )


application = webapp.WSGIApplication([
    (r'/.*', Login)
],debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

